package com.jingdianjichi.oss.service;

import com.jingdianjichi.oss.adapter.StorageAdapter;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Base64;
import java.util.List;

/*
 * 抽取fileService,相当于防腐层
 * */
@Service
public class FileService {


    private final StorageAdapter storageAdapter;

    public FileService(StorageAdapter storageAdapter) {
        this.storageAdapter = storageAdapter;
    }

    /*
     * 列出所有桶
     * */
    public List<String> listBucket() {
        return storageAdapter.listBucket();
    }

    /*
     * 获取文件的url
     * */
    public String getUrl(String buckName, String objectName) {
        return storageAdapter.getUrl(buckName, objectName);
    }

    /*
     * 上传文件
     * */
    public String uploadFile(MultipartFile multipartFile, String buckName, String objectName) {
        storageAdapter.uploadFile(multipartFile, buckName, objectName);
        objectName = objectName + "/" + multipartFile.getOriginalFilename();
        return storageAdapter.getUrl(buckName, objectName);
    }
}
